<template>
  <el-dialog :title="!dataForm.levelId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="等级名称" prop="levelName">
        <el-input v-model="dataForm.levelName" placeholder="等级名称"></el-input>
      </el-form-item>
      <el-form-item label="所需最小积分" prop="minPoints">
        <el-input v-model="dataForm.minPoints" placeholder="所需最小积分"></el-input>
      </el-form-item>
      <el-form-item label="所需最大积分" prop="maxPoints">
        <el-input v-model="dataForm.maxPoints" placeholder="所需最大积分"></el-input>
      </el-form-item>
      <el-form-item label="等级特权描述" prop="levelPrivilege">
        <el-input v-model="dataForm.levelPrivilege" placeholder="等级特权描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        levelId: 0,
        levelName: '',
        minPoints: '',
        maxPoints: '',
        levelPrivilege: ''
      },
      dataRule: {
        levelName: [
          { required: true, message: '等级名称不能为空', trigger: 'blur' }
        ],
        minPoints: [
          { required: true, message: '所需最小积分不能为空', trigger: 'blur' }
        ],
        maxPoints: [
          { required: true, message: '所需最大积分不能为空', trigger: 'blur' }
        ],
        levelPrivilege: [
          { required: true, message: '等级特权描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.levelId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.levelId) {
          this.$http({
            url: this.$http.adornUrl(`/admin/userLevel/info/${this.dataForm.levelId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.levelName = data.userLevel.levelName
              this.dataForm.minPoints = data.userLevel.minPoints
              this.dataForm.maxPoints = data.userLevel.maxPoints
              this.dataForm.levelPrivilege = data.userLevel.levelPrivilege
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/admin/userLevel/${!this.dataForm.levelId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'levelId': this.dataForm.levelId || undefined,
              'levelName': this.dataForm.levelName,
              'minPoints': this.dataForm.minPoints,
              'maxPoints': this.dataForm.maxPoints,
              'levelPrivilege': this.dataForm.levelPrivilege
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
